package com.xiaoma.myaudience.biz.task;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.http.Header;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import android.content.Context;

import com.xiaoma.myaudience.biz.model.CaptureModel;
import com.xiaoma.myaudience.biz.util.Constant;
import com.xiaoma.myaudience.util.HttpUtils;
import com.xiaoma.myaudience.util.Logger;
import com.xiaoma.myaudience.util.model.ModelUtils;
import com.xiaoma.myaudience.util.task.BaseDataAsyncTask;

/**
 * 异步上传自拍信息
 * 
 */
public class CaptureTask extends BaseDataAsyncTask<String, Void, Map<String, Object>>{
    private final static String TAG = "ActivitysTask";
    
    public CaptureTask(Context context, int taskType, DataAsyncCallback callback) {
        super(context, taskType, callback);
    }


    @Override
    protected Map<String, Object> doInBackground(String... params) {  
    	if (params == null) {
			return null;
		}
    	
    	Map<String, Object> result = null;
        try {            
        	String cookiestr = Constant.LOGIN_COOKIE1+"="+params[4]+";"+Constant.LOGIN_COOKIE2+"="+params[5];
			Header[] headers = new BasicHeader[2];//("X-Online-Host",  format_CMWAP_ServerName(strPath));  
			headers[0]=new BasicHeader("Cookie", cookiestr);
			headers[1]=new BasicHeader("Accept-Language","zh-CN");
			
        	List<NameValuePair> values = new ArrayList<NameValuePair>();
        	boolean istext = "3".equals(params[9]);
        	values.add(new BasicNameValuePair(CaptureModel.PARAM_TYPE, istext ? "subtext":"subpic"));
        	if (!istext) {
        		values.add(new BasicNameValuePair(CaptureModel.PARAM_PICID, params[8]));
        		values.add(new BasicNameValuePair(CaptureModel.PARAM_ISAUTO, "1".equals(params[9]) ? "1" : "2"));
			}
        	values.add(new BasicNameValuePair(CaptureModel.PARAM_CONTENT, params[0]));
        	values.add(new BasicNameValuePair(CaptureModel.PARAM_STARLEVEL, params[7]));
        	values.add(new BasicNameValuePair(CaptureModel.PARAM_X, params[1]));
        	values.add(new BasicNameValuePair(CaptureModel.PARAM_Y, params[2]));
        	values.add(new BasicNameValuePair(CaptureModel.PARAM_LOCATION, params[6]));
        	//values.add(new BasicNameValuePair(CaptureModel.PARAM_EVENT, params[3]));
        	if(!("1".equals(params[3]) || params[3]==null)){
        		values.add(new BasicNameValuePair(CaptureModel.PARAM_EVENT, params[3]));
        	}
        	String str = HttpUtils.getHttpStringResult(getHttpClient(), Constant.URL_COMMONT, values, headers, HttpUtils.POST, "GBK");
        	JSONObject jsonObject = new JSONObject(str);
//        	JSONObject jsonObject = HttpUtils.getHttpJSONObjectResult(getHttpClient(), 
//        			Constant.URL_COMMONT, values, HttpUtils.POST);
        	if (jsonObject != null) {
        		result = ModelUtils.json2Map(jsonObject);
			}
        } catch (Exception e) {
            Logger.e(TAG, "doInBackground error!!!", e);
        } finally {
            closeHttpClient();
        }
        
        return result;
    }
}
